package ai.plantdata.dao.strategy.impl;

import ai.plantdata.dao.dto.ConceptAssociateConceptTable;
import ai.plantdata.dao.strategy.SeparateHandle;
import ai.plantdata.dao.util.JdbcUtils;
import ai.plantdata.graph.api.schema.ConceptDefinition;
import ai.plantdata.graph.mongo.MongoGraphStore;
import ai.plantdata.vo.DataSourceConfig;
import ai.plantdata.dao.util.BeanTransform;
import ai.plantdata.vo.SeparateTable;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: WangHui
 * @Date: 2021/7/19 10:37
 * @Description:
 */
public class ConceptAssociateConcept implements SeparateHandle {
    @Override
    public List<ConceptDefinition> schemaHandle(SeparateTable separateDTO, DataSourceConfig dataSourceConfig, MongoGraphStore store) {
        try {
            ConceptAssociateConceptTable table = BeanTransform.separateTableToConceptAssociateConceptTable(separateDTO);
            String tableName = table.getTableName();
            String associationField = table.getAssociationField();
            String nameField = table.getNameField();
            String meanTagField = table.getMeanTagField();
            String sql;
            Connection conn = JdbcUtils.getConnection(dataSourceConfig);
            List<ConceptDefinition> conceptDefinitions = new ArrayList<>();
            if (meanTagField == null) {
                sql = "select %s , %s from %s";
                sql = String.format(sql, nameField, associationField, tableName);
                PreparedStatement ps = conn.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    ConceptDefinition conceptDefinition = ConceptDefinition.create(rs.getObject(nameField, String.class));
                    Object object = rs.getObject(associationField);
                    Map<String, String> map = getAssociate(conn, table.getAssociateDTO(), object);
                    if (map != null) {
                        conceptDefinition.setParent(ConceptDefinition.create(map.get("name"), map.get("meanTag")));
                    }
                    conceptDefinitions.add(conceptDefinition);
                }
                JdbcUtils.close(ps, rs);
            } else {
                sql = "select %s , %s, %s from %s";
                sql = String.format(sql, nameField, meanTagField, associationField, tableName);
                PreparedStatement ps = conn.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    ConceptDefinition conceptDefinition = ConceptDefinition.create(rs.getObject(nameField, String.class), rs.getObject(meanTagField, String.class));
                    Object object = rs.getObject(associationField);
                    Map<String, String> map = getAssociate(conn, table.getAssociateDTO(), object);
                    if (map != null) {
                        conceptDefinition.setParent(ConceptDefinition.create(map.get("name"), map.get("meanTag")));
                    }
                    conceptDefinitions.add(conceptDefinition);
                }
                JdbcUtils.close(ps, rs);
                conn.close();
            }
            if (!conceptDefinitions.isEmpty()) {
                store.saveSchema(conceptDefinitions);
                conceptDefinitions.clear();
            }
            return conceptDefinitions;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 概念表中无实体
     *
     * @param separateDTO
     * @param dataSourceConfig
     * @return
     */
    @Override
    public void entityHandle(SeparateTable separateDTO, DataSourceConfig dataSourceConfig, MongoGraphStore store) {
    }
}
